﻿namespace MicroCloud.Identity.Database
{
    /// <summary>
    /// 角色组织机构信息数据实体映射配置
    /// </summary>
    public class RoleOrganizationConfiguration : EntityTypeConfigurationBase<RoleOrganization, long>
    {
        /// <summary>
        /// 重写以实现实体类型各个属性的数据库配置
        /// </summary>
        /// <param name="builder">实体类型创建器</param>
        public override void Configuration(EntityTypeBuilder<RoleOrganization> builder)
        {
            //builder.HasIndex(m => new { m.RoleId, m.OrganizationId }).HasDatabaseName("IDX_RoleOrganization_Ids").IsUnique();
            builder.HasIndex(m => new { m.RoleId, m.OrganizationId }).SetIndex("Ids", true);

            //builder.HasOne(ur => ur.Organization).WithMany(r => r.RoleOrganizations).HasForeignKey(m => m.OrganizationId).HasConstraintName("FK_RoleOrganization_OrganizationId").IsRequired();
            //builder.HasOne(ur => ur.Role).WithMany(u => u.RoleOrganizations).HasForeignKey(m => m.RoleId).HasConstraintName("FK_RoleOrganization_RoleId").IsRequired();
            builder.HasOne(ur => ur.Organization).WithMany(r => r.RoleOrganizations).HasForeignKey(m => m.OrganizationId).SetForeignKey("OrganizationId");
            builder.HasOne(ur => ur.Role).WithMany(u => u.RoleOrganizations).HasForeignKey(m => m.RoleId).SetForeignKey("RoleId");

            builder.HasQueryFilter(o => o.Organization.DeletedTime == null && o.Role.DeletedTime == null);

            ConfigurationAppend(builder);
        }

    }
}
